<template>
	<view class="main-content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex == index}"
			 @click="tabClick(index)">
				{{item.text}}
			</view>
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData('reachBottom')">
					<!-- 空白页 -->
					<empty v-if="tabItem.itemList.length == 0"></empty>
					
					<!-- 消息列表 -->
					<view>
						<view class="notice-item" v-for="(item, index) in tabItem.itemList" :key="index" @click="toDetailPage(item)">
							<text class="time">{{item.time}}</text>
							<view class="content">
								<text class="title">{{item.title}}</text>
								<view class="img-wrapper">
									<image class="pic" :src="localFileStorage ? frontBaseUrl + item.coverImg : item.coverImg"></image>
								</view>
								<text class="introduce">
									{{item.summary}}
								</text>
								<view class="bot b-t">
									<text>查看详情</text>
									<text class="more-icon iconfont iconxiangyou"></text>
								</view>
							</view>
						</view>
					</view>
					
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty";
	import {
		doPostJson,
		doGet,
		showInfoToast,
		showSuccessToast,
		nullToStr,
		FRONT_BASE_URL,
		LOCAL_FILE_STORAGE,
		REFRESH_ORDER,
		timeAgo
	} from '@/common/util.js'
	import * as ResponseStatus from '@/common/response-status.js'
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '系统通知',
						loadingType: 'more',
						itemList: []
					},
					{
						state: 1,
						text: '我的消息',
						loadingType: 'more',
						itemList: []
					}
				],
				urls: {
					noticeUrl: '/notice/any/pager-cond',
					messageUrl: '/user-usermessage/user/pager-cond'
				},
				pager: {
					pageNo: 1,
					pageSize: 10,
					isActive: 0,
					sortColumn: 'createTime',
					sortOrder: 'desc'
				},
				frontBaseUrl: FRONT_BASE_URL,
				localFileStorage: LOCAL_FILE_STORAGE
			}
		},
		onLoad(options) {
			
			this.loadData('init');
		},
		//下拉刷新
		onPullDownRefresh() {
			console.log('refresh')
			this.pager.pageNo = 1
			this.loadData('pullDown')
		},
		//加载更多
		onReachBottom() {
			this.loadData('reachBottom');
		},
		methods: {
			//获取消息列表
			loadData(type) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return
				}
				console.log(navItem.loadingType)
				if (navItem.loadingType === 'nomore' && type === 'reachBottom') {
					return
				}
				if (type === 'reachBottom') {
					this.pager.pageNo += 1
				}
				navItem.loadingType = 'loading';
				let url = this.urls.noticeUrl;
				if (index === 1) {
					url = this.urls.messageUrl;
				}
				doPostJson(url, this.pager, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						// 判断是否还有数据， 有改为 more， 没有改为noMore 
						navItem.loadingType = this.pager.pageNo * this.pager.pageSize >= res.data.data.total ? 'nomore' : 'more'
						let tempRows = nullToStr(res.data.data.rows);
						let rows = []
						tempRows.forEach(item => {
							let createTime = item.createTime
							if (index === 1) {
								createTime = item.userMessageCreateTime
							}
							let createTimeDate = new Date(createTime.replace(/-/g,'/')).getTime();
							item.time = timeAgo(createTimeDate);
							if (index === 1) {
								item.title = item.messageTitle
								item.summary = item.messageSummary
								item.content = item.messageContent
								item.id = item.messageId
								item.coverImg = item.messageCoverImg
								item.createTime = item.userMessageCreateTime
							}
							rows.push(item);
						});
						if (type === 'init') {
							navItem.itemList = rows;
						} else if (type === 'pullDown') {
							uni.stopPullDownRefresh();
							navItem.itemList = rows;
						} else if (type === 'reachBottom') {
							if (rows.length > 0) {
								navItem.itemList = navItem.itemList.concat(rows);
							}
						}
					} else {
						showInfoToast(res.data.message);
					}
				}).catch(err => {
					console.log(err);
				})
			},
			//swiper 切换
			changeTab(e) {
				const index = e.target.current;
				this.tabClick(index);
			},
			//顶部tab点击
			tabClick(index) {
				if (this.tabCurrentIndex != index) {
					this.pager.pageNo = 1
					this.tabCurrentIndex = index
					this.navList[index].loadingType = 'more'
					if (index === 0) {
						// 系统通知
						this.pager.sortColumn = 'createTime'
					} else {
						// 用户消息
						this.pager.sortColumn = 'userMessageCreateTime'
					}
					this.loadData('init')
				}
			},
			/**
			 * 查看消息详情
			 * @param {Object} item
			 */
			toDetailPage(item) {
				uni.navigateTo({
					url: '/pages/message/message_detail?itemData=' + encodeURIComponent(JSON.stringify(item))
				})
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.main-content {
		/* background: $page-color-base; */
		background-color: #f7f7f7;
		height: 100%;
	}
	
	.swiper-box {
		height: calc(100% - 40px);
	}
	
	.list-scroll-content {
		height: 100%;
	}
	
	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		position: relative;
		z-index: 10;
	
		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
	
			&.current {
				color: $base-color;
	
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
	
	.uni-swiper-item {
		height: auto;
	}

	.notice-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.time {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 80upx;
		padding-top: 10upx;
		font-size: 26upx;
		color: #7d7d7d;
	}

	.content {
		width: 710upx;
		padding: 0 24upx;
		background-color: #fff;
		border-radius: 4upx;
	}

	.title {
		display: flex;
		align-items: center;
		height: 90upx;
		font-size: 32upx;
		color: #303133;
	}

	.img-wrapper {
		width: 100%;
		height: 260upx;
		position: relative;
	}

	.pic {
		display: block;
		width: 100%;
		height: 100%;
		border-radius: 6upx;
	}

	.cover {
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, .5);
		font-size: 36upx;
		color: #fff;
	}

	.introduce {
		display: inline-block;
		padding: 16upx 0;
		font-size: 28upx;
		color: #606266;
		line-height: 38upx;
	}

	.bot {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 80upx;
		font-size: 24upx;
		color: #707070;
		position: relative;
	}

	.more-icon {
		font-size: 32upx;
		color: $font-color-light;
	}
</style>
